﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace WebMonitoring
{
    public class WebStatus
    {
        /// <summary>
        /// Return group of status of a website (by using http request). 
        /// 0: Exception. 1: Sucessful. 2: Redirection. 3: Client Error. 4: Server Errors
        /// </summary>
        /// <param name="url"></param>
        /// <returns>int</returns>
        public static  int GetGroupOfPageStatus(string url)
        {
            int iStatus = 0;
            try
            {
                // Creates an HttpWebRequest for the specified URL. 
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                // Sends the HttpWebRequest and waits for a response.
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.Created ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.Accepted ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.NoContent ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.NonAuthoritativeInformation ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.ResetContent ||
                    myHttpWebResponse.StatusCode == HttpStatusCode.PartialContent)
                {
                    // Sucessful
                    iStatus = 1;
                }

                else if (myHttpWebResponse.StatusCode == HttpStatusCode.MovedPermanently ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.Found ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.UseProxy ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.TemporaryRedirect)
                {
                    // Redirecttion
                    iStatus = 2;
                }

                else if (myHttpWebResponse.StatusCode == HttpStatusCode.BadRequest ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.Forbidden ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.NotFound ||
                        myHttpWebResponse.StatusCode == HttpStatusCode.MethodNotAllowed)
                {
                    // Client Error
                    iStatus = 3;
                }

                else
                {
                    //Server Error
                    iStatus = 4;
                }
                // Releases the resources of the response.
                myHttpWebResponse.Close();

            }
            catch (WebException)
            {
                iStatus = 0;
            }
            catch (Exception)
            {
                iStatus = 0;
            }

            return iStatus;
        }

        /// <summary>
        /// Get detailed status of a page (by using http request)
        /// </summary>
        /// <returns>string</returns>
        public static  string GetDetailedPageStatus(string url)
        {
            string strStatus = "Cann't get info";
            try
            {
                // Creates an HttpWebRequest for the specified URL. 
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                // Sends the HttpWebRequest and waits for a response.
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

                // Get status
                strStatus = myHttpWebResponse.StatusCode.ToString();
                // Releases the resources of the response.
                myHttpWebResponse.Close();

            }
            catch (WebException wex)
            {
                strStatus = wex.Message;
            }
            catch (Exception ex)
            {
                strStatus = ex.Message;
            }

            return strStatus;
        }


    }
}
